<?php
// resize.php?t=c/

function getExtension($str) {     
	$i = strrpos($str,".");
	if (!$i) {
		return "";
	}
	$l = strlen($str) - $i;
	$ext = substr($str,$i+1,$l);
	return strtolower($ext);
}
	# Avaible image
	if(!file_exists($_GET['url'])) $_GET['url'] =  "../".$_GET['url'];
	
	if($_GET['url'] == "") $_GET['url'] = "images/non-img.jpg";
	if( !isset ($_GET['url'])) {
		$_GET['url'] = "images/non-img.jpg";
	}
	list($width_orig, $height_orig) = @getimagesize($_GET['url']);
	if ($width_orig < 10 || $height_orig < 10) $_GET['url'] = "images/non-img.jpg";

	$type = $_GET['t'];
	switch ($type) {
		case 'cut' : 
			$width = $_GET['width'];
			$height = $_GET['height'];
			$filename = $_GET['url'];
			
			list($width_orig, $height_orig) = getimagesize($filename);
			
			$image_p = imagecreatetruecolor($width, $height);
			$ratio_orig = $width_orig/$height_orig;
			if ($width/$height > $ratio_orig) {
			   $height = $width/$ratio_orig;
			} else {
				$width = $height*$ratio_orig;
			   
			}

			switch(getExtension($filename)) {
				case 'jpeg' :
				case 'jpg' :
					header('content-type: image/jpeg'); 
					$image = imagecreatefromjpeg($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagejpeg($image_p, null, 100);
										
				break;
				case 'png' :
					header('content-type: image/png'); 
					$image = imagecreatefrompng($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagepng($image_p, null, 100);
				break;
				case 'gif' :
					header('content-type: image/gif'); 
					$image = imagecreatefromgif($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagegif($image_p, null, 100);
				break;				
			}

	break ;
	case 'ratio' :
			$width = $_GET['width'];
			$height = $_GET['height'];
			$filename = $_GET['url'];
			
			list($width_orig, $height_orig) = getimagesize($filename);
			
			if ($height > $height_orig)  {
				$height = $height_orig ;
			}
			
			$ratio_orig = $width_orig/$height_orig;
			
			if ($width/$height > $ratio_orig) {
				$width = $height*$ratio_orig;			   
			} else {
				$height = $width/$ratio_orig;
	   
			}
			$image_p = imagecreatetruecolor($width, $height);
		
		switch(getExtension($filename)) {
				case 'jpeg' :
				case 'jpg' :
					header('content-type: image/jpeg'); 
					$image = imagecreatefromjpeg($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagejpeg($image_p, null, 100);
				break;
				case 'png' :
					header('content-type: image/png'); 
					$image = imagecreatefrompng($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagepng($image_p, null, 100);
				break;
				case 'gif' :
					header('content-type: image/gif'); 
					$image = imagecreatefromgif($filename);
					imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig);
					imagegif($image_p, null, 100);
				break;				
			}
	break ;
	}
	@imagedestroy($image);
	@imagedestroy($image_p);
?> 
